//
//  CoreDataController.m
//  CoreDataTest
//
//  Created by wangrui on 14-2-28.
//  Copyright (c) 2014年 Ourui. All rights reserved.
//

#import "CoreDataController.h"

@implementation CoreDataController

- (NSManagedObjectContext *)mainObjectCtx{
    if (!_mainObjectCtx) {
        NSManagedObjectContext *moc = [[NSManagedObjectContext alloc]init];
        [moc setPersistentStoreCoordinator:self.coordinator];
        self.mainObjectCtx = moc;
    }
    
    return _mainObjectCtx;
}


- (NSPersistentStoreCoordinator *)coordinator{
    if (!_coordinator) {
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"Class" withExtension:@"momd"];
        NSManagedObjectModel *model = [[NSManagedObjectModel alloc]initWithContentsOfURL:url];
        
        NSPersistentStoreCoordinator *c = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
        NSURL *storeUrl = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
                                                                 inDomains:NSUserDomainMask]
                           lastObject] URLByAppendingPathComponent:@"class.sqlite"];
        
        [c addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:nil];
        
        self.coordinator = c;
    }
    
    return _coordinator;
}


@end
